package com.open.capacity.user.service.impl;

import com.open.capacity.commons.Result;
import com.open.capacity.user.dao.SysConfigDao;
import com.open.capacity.user.model.SysConfig;
import com.open.capacity.user.service.SysConfigService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;


@Slf4j
@Service
public class SysConfigServiceImpl implements SysConfigService {

    @Autowired
    private SysConfigDao configDao;


    @Transactional(rollbackFor = Exception.class)
    @Override
    public void update(SysConfig config) {
        config.setUpdateTime(new Date());

        configDao.updateByOps(config);
        log.info("修改配置：{}", config);
    }


    @Override
    public SysConfig findById(String id) {
        return configDao.findById(id);
    }

    @Override
    public SysConfig findByKey(String id) {
        return configDao.findByKey(id);
    }

    @Override
    public Result saveOrUpdate(SysConfig config, String keyValue) {
        int i = 0;

        SysConfig model = configDao.findById(keyValue);
        if (model != null) {
            config.setId(keyValue);
            config.setUpdateTime(new Date());
            i = configDao.updateByOps(config);
        }

        return i > 0 ? Result.succeed("操作成功") : Result.failed("操作失败");
    }
}
